package com.zou.lotterylucky.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zou.lotterylucky.R
import com.zou.lotterylucky.activity.SettingInnerActivity
import com.zou.lotterylucky.data.RedeemItem

class RedeemAdapter(
    private var dataSet: MutableList<RedeemItem>,
    private var activity: SettingInnerActivity
) : RecyclerView.Adapter<RedeemAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val dateLbl: TextView = view.findViewById(R.id.dateLbl)
        val salesLbl: TextView = view.findViewById(R.id.salesLbl)
        val commissionLbl: TextView = view.findViewById(R.id.commissionLbl)
        val prizeLbl: TextView = view.findViewById(R.id.prizeLbl)
        val redemptionLbl: TextView = view.findViewById(R.id.redemptionLbl)
        val settlementLbl: TextView = view.findViewById(R.id.settlementLbl)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_redeem, viewGroup, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        val redeemItem = dataSet[position]

        viewHolder.dateLbl.text = redeemItem.date
        viewHolder.salesLbl.text = String.format("%.2f", redeemItem.subTotal)
        viewHolder.commissionLbl.text = String.format(
            "%.2f",
            redeemItem.getCommissionValue()
        ) + "(" + redeemItem.commission.toString() + "%)"
        viewHolder.prizeLbl.text = String.format("%.2f", redeemItem.prizes)
        viewHolder.redemptionLbl.text = String.format("%.2f", redeemItem.redeemAmount)
        viewHolder.settlementLbl.text = String.format("%.2f", redeemItem.getSettlement())

        viewHolder.prizeLbl.setOnClickListener {
            activity.displayDialog(redeemItem.date)
        }
    }

    override fun getItemCount() = dataSet.size

    fun updateAdapter(newList: MutableList<RedeemItem>) {
        dataSet.clear()
        dataSet.addAll(newList)
        notifyDataSetChanged()
    }
}